package com.example.zlm.ndk;

import android.util.Base64;

import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.SecureRandom;
import java.security.spec.AlgorithmParameterSpec;
import java.security.spec.InvalidKeySpecException;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;

import static javax.crypto.Cipher.*;

/**
 * Created by ZLM on 2016/7/27.
 * TODO: Write something
 */
public class DESUtils {


    /**
     * DES算法密钥
     */
    private static final byte[] DES_KEY = { 21, 1, -110, 82, -32, -85, -128, -65 };

    private Cipher cipher;
    private SecretKey secretKey;
    private String test;

    private SecureRandom sr = new SecureRandom();

    public void init() throws InvalidKeySpecException {
        try {
            cipher = Cipher.getInstance("DES");
            DESKeySpec desKey = null;
            try {
                desKey = new DESKeySpec(DES_KEY);
            } catch (InvalidKeyException e) {
                e.printStackTrace();
            }
            //创建一个密匙工厂，然后用它把DESKeySpec转换成
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
            secretKey = keyFactory.generateSecret(desKey);

        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        }
    }

    public String  DESEncode(String  s){
        String ss=null;
        try {
                cipher.init(Cipher.ENCRYPT_MODE,secretKey,sr);

            try {
                ss=asHex(cipher.doFinal(s.getBytes()));

            } catch (IllegalBlockSizeException e) {
                e.printStackTrace();
            } catch (BadPaddingException e) {
                e.printStackTrace();
            }
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        }

        return ss;
    }

    public String  DESDecode(String  s){
        String ss=null;
        try {
                cipher.init(Cipher.DECRYPT_MODE,secretKey,sr);
            try {
                ss=new String(cipher.doFinal(asBin(s)));
            } catch (IllegalBlockSizeException e) {
                e.printStackTrace();
            } catch (BadPaddingException e) {
                e.printStackTrace();
            }
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        }

        return ss;
    }

    /**
     * 转16进制
     **/
    private static String asHex(byte buf[]) {
        StringBuffer strbuf = new StringBuffer(buf.length * 2);
        int i;
        for (i = 0; i < buf.length; i++) {
            if (((int) buf[i] & 0xff) < 0x10)//小于十前面补零
                strbuf.append("0");
            strbuf.append(Long.toString((int) buf[i] & 0xff, 16));
        }
        return strbuf.toString();
    }

    /**
     * 转2进制
     **/
    private static byte[] asBin(String src) {
        if (src.length() < 1)
            return null;
        byte[] encrypted = new byte[src.length() / 2];
        for (int i = 0; i < src.length() / 2; i++) {
            int high = Integer.parseInt(src.substring(i * 2, i * 2 + 1), 16);//取高位字节
            int low = Integer.parseInt(src.substring(i * 2 + 1, i * 2 + 2), 16);//取低位字节
            encrypted[i] = (byte) (high * 16 + low);
        }
        return encrypted;
    }

}
